package ipdesk.model.report.invade.action;

import ipdesk.model.common.dao.CommonDao;
import ipdesk.model.report.invade.bean.InvadeInfoBean;
import ipdesk.model.report.invade.dao.InvadeInfoDao;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.itf.dao.ECGDao;
import ecg.util.FileService;
import ecg.util.SqlMapLocator;
import ecg.util.StringUtils;
import ecg.util.Time;

public class InvadeInfoRegAction extends ActionSupport implements Preparable, ModelDriven<InvadeInfoBean>
{
	private ECGDao ecgDao;
	private InvadeInfoBean invadeInfoBean;
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		String returnFlag = "SUCCESS";
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		invadeInfoBean.setPath_name(basePath);
		invadeInfoBean.setRegTime(regTime);
		invadeInfoBean.setDifficulty(StringUtils.nl2br(invadeInfoBean.getDifficulty()));
		//invadeInfoBean.setSurveyContent(StringUtils.nl2br(invadeInfoBean.getSurveyContent()));
		//invadeInfoBean.setBustPlan(StringUtils.nl2br(invadeInfoBean.getBustPlan()));
		
		// desk
		if(!invadeInfoBean.getDc1().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc1()); }
		if(!invadeInfoBean.getDc2().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc2()); }
		if(!invadeInfoBean.getDc3().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc3()); }
		if(!invadeInfoBean.getDc4().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc4()); }
		if(!invadeInfoBean.getDc5().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc5()); }
		if(!invadeInfoBean.getDc6().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc6()); }
		System.out.println("데스크코드 : " + invadeInfoBean.getDeskCode());
		int companySeqCount;
		

			// 상담정보 insert.
			int lastInvadeSeq = InvadeInfoDao.getInstance().invadeInfoReg(invadeInfoBean);
			
			// 마지막으로 insert된 consult_seq 값을 빈 객체에 저장.		
			invadeInfoBean.setInvadeSeq(lastInvadeSeq);
			
			if(lastInvadeSeq != 0 && invadeInfoBean.getUpload() != null)
			{
				String uploadFileName = invadeInfoBean.getUploadFileName();
				String realFileName = fileService.saveFile(invadeInfoBean.getUpload(), totalPath, uploadFileName);
				invadeInfoBean.setUploadFileName(realFileName);
				invadeInfoBean.setFile_category("FLE12");
				
				InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
			}
			
			if(lastInvadeSeq != 0 && invadeInfoBean.getUploadED() != null) {
				String uploadFileName = invadeInfoBean.getUploadEDFileName();
				String realFileName = fileService.saveFile(invadeInfoBean.getUploadED(), totalPath, uploadFileName);
				invadeInfoBean.setUploadFileName(realFileName);
				invadeInfoBean.setFile_category("FLE31");
				
				InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
			}
			
			returnFlag = "SUCCESS";
		
		return returnFlag;
	}
	
	public void prepare() throws Exception
	{
		invadeInfoBean = new InvadeInfoBean();
	}
	
	public InvadeInfoBean getModel()
	{
		return invadeInfoBean;
	}
	
	/*public void validate()
	{
		String contentType = invadeInfoBean.getUploadContentType();
		long fileSize = 0;
		
		if(invadeInfoBean.getUpload() != null)
		{
			fileSize = invadeInfoBean.getUpload().length();
		}
		
		if(!fileService.contentTypeValidate(contentType))
		{
			addFieldError("upload", "업로드할 수 없는 파일 형식입니다");
		}
		
		if(fileSize > Long.parseLong(getText("upload.size")))
		{
			addFieldError("upload", "업로드 용량을 초과하였습니다.");
			return;
		}
		
	}*/
}
